<?php
/**
 * 新浪微博用户Model
 * @author solar
 */
class SinaModel extends BaseModel 
{
	public static $name='sina';
	public static $primary='sina_id';
	
	public function __construct() {
		parent::__construct(CoreFactory::db(),self::$name,self::$primary);
	}
	
	/**
	 * 创建新浪微博用户
	 * @author solar
	 * @param int $user_id 新浪微博用户ID
	 * @return array screen_name profile_image_url
	 */
	public function createSinaUser($user_id) {
		$client=new SaeTClientV2(WB_AKEY,WB_SKEY,CoreFactory::session()->get('sina_token') ,'');
		$sinaUserInfo=$client->show_user_by_id($user_id);
		if(array_key_exists('error_code',$sinaUserInfo)) return false;
		$sinaRow['sina_id']=$sinaUserInfo['id'];
		$sinaRow['screen_name']=$sinaUserInfo['screen_name'];
		$sinaRow['name']=$sinaUserInfo['name'];
		$sinaRow['province']=$sinaUserInfo['province'];
		$sinaRow['city']=$sinaUserInfo['city'];
		$sinaRow['location']=$sinaUserInfo['location'];
		$sinaRow['url']=$sinaUserInfo['url'];
		$sinaRow['profile_image_url']=$sinaUserInfo['profile_image_url'];
		$sinaRow['domain']=$sinaUserInfo['domain'];
		$sinaRow['gender']=$sinaUserInfo['gender'];
		$sinaRow['created_at']=strtotime($sinaUserInfo['created_at']);
		$sinaRow['allow_all_act_msg']=$sinaUserInfo['allow_all_act_msg'];
		$sinaRow['geo_enabled']=$sinaUserInfo['geo_enabled'];
		$sinaRow['verified']=$sinaUserInfo['verified'];
		if(!$this->insert($sinaRow)) return false;
		return array('screen_name'=>$sinaRow['screen_name'],'profile_image_url'=>$sinaRow['profile_image_url']);
	}
	
	/**
	 * 关联应用用户ID
	 * @author solar
	 * @param int $sina_id
	 * @param int $u_id
	 */
	public function setUserID($sina_id,$u_id) {
		return $this->update(array('u_id'=>$u_id),$sina_id);
	}
	
	/**
	 * 更新用户的token
	 * @author solar
	 * @param int $sina_id
	 * @param string $sina_token
	 */
	public function updateToken($sina_id,$sina_token) {
		return $this->update(array('token'=>$sina_token),$sina_id);
	}
}

?>